pro update_cmag
common cmagcom, cmagnew, cmagold
common cmag, status

widget_control, status.btns[0], set_value=cmagnew[status.ind].mincolor
widget_control, status.btns[1], set_value=cmagnew[status.ind].maxcolor
widget_control, status.btns[2], set_value=cmagnew[status.ind].bincolor
widget_control, status.btns[3], set_value=cmagnew[status.ind].minflux
widget_control, status.btns[4], set_value=cmagnew[status.ind].maxflux
widget_control, status.btns[5], set_value=cmagnew[status.ind].binflux

wh=where(strmatch(status.fnames, cmagnew[status.ind].f1), ct)
if ct NE 0 then begin
for i=0, n_elements(status.fluxesbtns)-1 do $
        widget_control, status.fluxesbtns2[i], sensitive=1
widget_control, status.fluxesbtns2[wh], sensitive=0
widget_control, status.fluxesbtns[wh], /set_button
endif

wh=where(strmatch('-'+status.fnames, cmagnew[status.ind].f2), ct)
if ct NE 0 then begin
for i=0, n_elements(status.fluxesbtns)-1 do $
        widget_control, status.fluxesbtns[i], sensitive=1
widget_control, status.fluxesbtns[wh], sensitive=0
widget_control, status.fluxesbtns2[wh], /set_button
endif




end

pro cmagadd_event, ev
common cmagcom, cmagnew, cmagold
common cmag, status

widget_control, ev.id, get_uvalue=uvalue
case uvalue of
'mincolor':cmagnew[status.ind].mincolor=ev.value
'maxcolor':cmagnew[status.ind].maxcolor=ev.value
'bincolor':cmagnew[status.ind].bincolor=ev.value
'minflux':cmagnew[status.ind].minflux=ev.value
'maxflux':cmagnew[status.ind].maxflux=ev.value
'binflux':cmagnew[status.ind].binflux=ev.value
'add':begin
	cmagnew=[cmagnew, cmagnew[n_elements(cmagnew)-1]]
	status.ind=n_elements(cmagnew)-1
      end
'del':begin
      if n_elements(cmagnew) GT 1 then begin
	if status.ind EQ 0 then begin
	   cmagnew=cmagnew[1:*]
	endif else $
        if status.ind EQ n_elements(cmagnew)-1 then begin
	   cmagnew=cmagnew[0:n_elements(cmagnew)-2]
	   status.ind=n_elements(cmagnew)-1
	endif else begin
	   cmagnew=[cmagnew[0:status.ind-1], cmagnew[status.ind+1:*]]
	endelse
	update_cmag
      endif

     end
'<<':begin
	status.ind=status.ind - 1 > 0
	update_cmag
     end
'>>':begin
	status.ind= (status.ind +1) < (n_elements(cmagnew)-1)
	update_cmag
     end
else:
endcase
widget_control, status.lbl, set_value='CMD '+rstring(status.ind+1)+$
                ' of '+rstring(n_elements(cmagnew))


wh=where(strmatch(status.fnames, uvalue), ct)
if ct NE 0 then begin
cmagnew[status.ind].f1=uvalue
for i=0, n_elements(status.fluxesbtns)-1 do $
	widget_control, status.fluxesbtns2[i], sensitive=1
widget_control, status.fluxesbtns2[wh], sensitive=0
endif

wh=where(strmatch('-'+status.fnames, uvalue), ct)
if ct NE 0 then begin
cmagnew[status.ind].f2=uvalue
for i=0, n_elements(status.fluxesbtns)-1 do $
        widget_control, status.fluxesbtns[i], sensitive=1
widget_control, status.fluxesbtns[wh], sensitive=0
endif



if strmatch(uvalue, 'OK') then begin
  if keyword_set(cmagnew) then cmagold=cmagnew
  widget_control, ev.top, /destroy
endif

if strmatch(uvalue, 'quit') then begin
	;if keyword_set(cmagold) then cmagnew=cmagold else delvarx, cmagnew
	widget_control, ev.top, /destroy
endif
end


pro cmagadd, base, s
common cmagcom, cmagnew, cmagold
common cmag, status

fluxnames=['NUV','FUV','BOL','J','H','K','SDSSu','SDSSg','SDSSr','SDSSi','SDSSz',$
        'johnsonU','johnsonB','johnsonV','johnsonR','johnsonI', 'QH0']

wh=where(s.fluxes GT 0, ct)
fnames=fluxnames[wh]
if ct LT 2 then begin
	print, 'SLUG: you need at least 2 fluxes to make a CMD'
	return
endif

if keyword_set(cmagold) then cmagnew=cmagold $
	else cmagnew={f1:'', f2:'', binflux:'100', maxflux:'50', minflux:'1', $
			maxcolor:'50', mincolor:'1', bincolor:'100'}

status={tlb:0L, fluxnames:fluxnames, fluxes:s.fluxes,fnames:fnames, $
	fluxesbtns:lonarr(n_elements(wh)), $
	fluxesbtns2:lonarr(n_elements(wh)), lbl:0L, ind:n_elements(cmagnew)-1,$
	btns:lonarr(6)}
	


if keyword_set(base) then begin
widget_control, base, tlb_get_offset=offs, tlb_get_size=sizes
  if keyword_set(x) EQ 0 then x=offs[0]+sizes[0]/2.
  if keyword_set(y) EQ 0 then y=offs[1]+sizes[1]/2.
end


status.tlb=widget_base(xsize=650, ysize=600, /col, title='Add Color Magnitude Diagrams')
row00=widget_base(status.tlb, /row, /align_center)
status.lbl=widget_label(row00, value='CMD '+rstring(n_elements(cmagnew))+$
		' of '+rstring(n_elements(cmagnew)))


base0=widget_base(status.tlb, col=2)
col0=widget_base(base0, col=4, xsize=320, /frame, /exclusive)

for i=0, n_elements(status.fluxesbtns)-1 do begin
   status.fluxesbtns[i]=widget_button(col0, value=fnames[i], uvalue=fnames[i])
endfor

col0=widget_base(base0, col=4, xsize=320, /frame, /exclusive)
for i=0, n_elements(status.fluxesbtns)-1 do begin
   status.fluxesbtns2[i]=widget_button(col0, value=fnames[i], uvalue='-'+fnames[i])
endfor

rowtxt=widget_base(status.tlb, /row)
rowtxt0=widget_base(rowtxt, row=2)
status.btns[0]=cw_field(rowtxt0, title='Mincolor', value='1     ', uvalue='mincolor',/all)
status.btns[1]=cw_field(rowtxt0, title='Maxcolor', value='50    ', uvalue='maxcolor',/all)
status.btns[2]=cw_field(rowtxt0, title='Bincolor', value='100   ', uvalue='bincolor',/all)
status.btns[3]=cw_field(rowtxt0, title=' Minflux', value='1     ', uvalue='minflux',/all)
status.btns[4]=cw_field(rowtxt0, title=' Maxflux', value='50    ', uvalue='maxflux',/all)
status.btns[5]=cw_field(rowtxt0, title=' Binflux', value='100   ', uvalue='binflux',/all)


sec_row=widget_base(status.tlb, /row, /align_center)
btn=widget_button(sec_row, value='<<', uvalue='<<')
btn=widget_button(sec_row, value='Delete', uvalue='del')
btn=widget_button(sec_row, value='Add', uvalue='add')
btn=widget_button(sec_row, value='>>', uvalue='>>')


bot_row=widget_base(status.tlb, /row, /align_center)
btn1=widget_button(bot_row, value='Cancel', uvalue='quit')
btn2=widget_button(bot_row, value='OK', uvalue='OK')


widget_control, status.tlb, /realize
widget_control, status.tlb, tlb_get_size=tlbsize

update_cmag
if keyword_set(x) then $
        centertlb, status.tlb, x-tlbsize[0]/2, y-tlbsize[1]/2, /nocenter, /device $
        else centertlb, status.tlb


xmanager, 'cmagadd', status.tlb, /no_block
end
